'''
Created on 2011-9-19

@author: 123kangta
'''
import unittest
from utils import jsonUtils as json


class User:
    def __init__(self, name, card):
        self.name = name
        self.idcard = card


class Idcard:
    def __init__(self, num):
        self.num = num


class ObjectToStrTest(unittest.TestCase):
    def setUp(self):
        self.user1 = User("user1", Idcard("123"))
        self.user2 = User("bbb", Idcard("456"))
        self.alluser = [self.user1, self.user2]

    def test_single_obj_success(self):
        expected = '{"idcard": {"num": "123"}, "name": "user1"}'
        result = json.obj_to_str(self.user1)
        self.assertEqual(result, expected)

    def test_complex_obj_success(self):
        expected = '[{"idcard": {"num": "123"}, "name": "user1"}, {"idcard": {"num": "456"}, "name": "bbb"}]'
        result = json.obj_to_str(self.alluser)
        self.assertEqual(result, expected)

    def test_str_obj_success(self):
        excepted = 'hello world'
        result = json.obj_to_str('hello world')
        self.assertEqual(result, excepted)

    def test_mutil_nested_obj_success(self):
        temp = User("aaa", Idcard("000"))
        temp.all = {self.user1, self.user2}
        result = json.obj_to_str(temp)
        expected = '{"all": [{"idcard": {"num": "123"}, "name": "user1"}, {"idcard": {"num": "456"}, "name": "bbb"}], "idcard": {"num": "000"}, "name": "aaa"}'
        self.assertEqual(result, expected)